package com.common.base.widget.image_watcher

import android.app.Activity
import android.net.Uri
import android.util.SparseArray
import android.widget.ImageView
import androidx.fragment.app.Fragment
import com.common.base.app.extras.load

object ImageWatcherUtils {

    fun createImageWatcher(activity: Activity?): ImageWatcherHelper {
        return createImageWatcher(activity, null)
    }

    /**
     * @param autoGifOnDismiss 关闭后原iv是否自动播放gif 默认为true
     */
    @JvmStatic
    fun createImageWatcher(activity: Activity?, fragment: Fragment?): ImageWatcherHelper {
        val iwHelper = ImageWatcherHelper
            .createDefault(activity, fragment)
        iwHelper?.setOnDimissListener(object : ImageWatcher.OnDimissListener {
            override fun onDimiss(isHasGif: Boolean, initUrlList: List<Uri?>?, views: SparseArray<ImageView>?) {
                if (!isHasGif)
                    return

                if (initUrlList.isNullOrEmpty())
                    return

                if (null == views || views.size() < 1)
                    return

                for (index in initUrlList.indices) {
                    val url = initUrlList[index].toString()
                    views[index].load(url)
                }
            }

        })
        return iwHelper
    }

    @JvmStatic
    fun showImageWatcher(iwHelper: ImageWatcherHelper, imgPath: String?) {
        val imgList = ArrayList<String>()
        imgList.add(imgPath ?: "")
        showImageWatcher(iwHelper, imgList, 0)
    }

    @JvmStatic
    fun showImageWatcher(iwHelper: ImageWatcherHelper?, imgList: List<String>?, selectPosition: Int) {
        if (null == iwHelper || imgList == null || imgList.isNullOrEmpty()) {
            //编译器不识别 commentImgList.isNullOrEmpty 为空的判断
            return
        }
        val imgs = ArrayList<Uri>()
        for (it in imgList)
            imgs.add(Uri.parse(it))

        iwHelper.show(imgs, selectPosition)
    }
}